package com.zpark.servlet_request;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求转发
 * 请求域对象
 */
@WebServlet("/sq03.do")
public class ServletRequestDemo03 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**
         * 浏览器发起请求访问ServletRequestDemo03，在ServletRequestDemo03中绑定数据到请求域对象
         * 转发到ServletRequestDemo04当中做下一步处理
         */
        System.out.println("请求访问的ServletRequestDemo03");
        // 绑定数据到请求域
        request.setAttribute("user", new String[]{"张三", "李四", "王五"});

        // 向浏览器输出内容
        response.getWriter().println("ServletRequestDemo03");

        // 将请求交给ServletRequestDemo04做处理
        /*
            转发步骤：
                1、获取转发器
                2、调用转发方法
         */
        // 获取转发器
        // getServletContext().getRequestDispatcher("")
        RequestDispatcher dispatcher = request.getRequestDispatcher("/sq04.do");
        // 调用转发方法
        dispatcher.forward(request, response);


    }
}
